<?php
use Laravel\Response;

class Api_Controller extends Base_Controller
{
    public $restful = true;

    public function get_result($lastTime = null, $days = 290)
    {
        if (Auth::guest())
        {
            return Response::json(array('code' => '-1', 'msg' => 'Not authorized'));
        }

        $timeADay = 24 * 60 * 60;

        if (is_null($lastTime) || $lastTime == 0)
        {
            $lastTime = time() + $timeADay;
        }

        $nextTime = date('Y-m-d', intval($lastTime) - $days * $timeADay);
        $lastTime = date('Y-m-d', $lastTime);

        Log::debug('Request Results ' . $nextTime);
        $data = User::results(Auth::user()->id, $lastTime, $nextTime);

        $data = array_map(function ($o) {
            return array(
                'id' => $o->id,
                'channel_id' => $o->channel_id,
                'weekday' => $o->weekday,
                'date' => date('d/m/Y', strtotime($o->date)),
                'time' => strtotime($o->date),
                'fn' => $o->first,
                'ln' => $o->last
            );
        }, $data);

        return Response::json(compact('data', 'lastTime', 'nextTime'));
    }

    public function get_collection($id = null)
    {
        $data = Collection::getAll($id);

        return Response::json($data);
    }

    public function put_collection()
    {
        $collection = Collection::find(Input::get('id'));

        if (is_null($collection))
        {
            $r['code']  = -1;
        }
        else
        {
            $numbers = Input::get('numbers');
            $collection->name = Input::get('name');
            $collection->numbers()->delete();

            foreach ($numbers as $n)
            {
                if (is_null($collection->numbers()->where_number($n)->first())) {
                    $collection->numbers()->insert(array('number' => $n));
                }
            }

            $collection->save();
            $r['code'] = 0;
            $r['data'] = array(
                'name' => Input::get('name'),
                'numbers' => $numbers
            );
        }

        return Response::json($r);
    }

    public function delete_collection()
    {
        $collection = Collection::find(Input::get('id'));
        if (!is_null($collection))
        {
            $collection->numbers()->delete();
            $collection->delete();
        }
    }

    public function post_collection ()
    {
        $collection = Collection::create(array('name' => Input::get('name')));
        foreach (Input::get('numbers', array()) as $n)
        {
            $collection->numbers()->insert(array('number' => $n));
        }

        return Response::json(array(
            'code' => 0,
            'data' => array(
                'id' => $collection->id,
                'name' => Input::get('name'),
                'numbers' => Input::get('numbers')
            )
        ));
    }

    public function get_maxline($collectionId)
    {
        $results = User::results(Auth::user()->id);
        $collection = Collection::getAll($collectionId);

        $maxLine = array(
            'f' => array(
                'start' => -1, # start line result ID
                'count' => 0
            ),
            'l' => array(
                'start' => -1,
                'count' => 0,
            )
        );

        $tracking = array(
            'f' => array(
                'start' => -1, # start line result ID
                'count' => 0,
                'inLine' => false
            ),
            'l' => array(
                'start' => -1,
                'count' => 0,
                'inLine' => false
            )
        );

        foreach ($results as $curResult)
        {
            if (in_array($curResult->first, $collection['numbers']))
            {
                if ($tracking['f']['inLine'])
                {
                    $tracking['f']['count']++;
                }
                else
                {
                    $tracking['f']['inLine'] = true;
                    $tracking['f']['start'] = $curResult->id;
                    $tracking['f']['count'] = 1;
                }
            }
            else
            {
                if ($tracking['f']['inLine'])
                {
                    $tracking['f']['inLine'] = false;
                    if ($maxLine['f']['count'] < $tracking['f']['count'])
                    {
                        $maxLine['f']['count'] = $tracking['f']['count'];
                        $maxLine['f']['start'] = $tracking['f']['start'];
                    }
                }
            }

            // Last
            if (in_array($curResult->last, $collection['numbers']))
            {
                if ($tracking['l']['inLine'])
                {
                    $tracking['l']['count']++;
                }
                else
                {
                    $tracking['l']['inLine'] = true;
                    $tracking['l']['start'] = $curResult->id;
                    $tracking['l']['count'] = 1;
                }
            }
            else
            {
                if ($tracking['l']['inLine'])
                {
                    $tracking['l']['inLine'] = false;
                    if ($maxLine['l']['count'] < $tracking['l']['count'])
                    {
                        $maxLine['l']['count'] = $tracking['l']['count'];
                        $maxLine['l']['start'] = $tracking['l']['start'];
                    }
                }
            }
        }

        return Response::json($maxLine);
    }
}